/* * ********************************
 * Jquery popup window
 * @Author: Ho Quang Tri
 * ******************************** */
var ie=document.all;
var ns6=document.getElementById && !document.all;
var popup_box = {	
        html : null,
        boxId : null,
        posX : 0,
        posY : 0,
        show_popup:function( boxId,e){			
            var _x = (typeof e.pageX != "undefined" ? e.pageX : e.clientX);		
            var _y = (typeof e.pageY != "undefined" ? e.pageY : e.clientY);		
            //var mouseY = (typeof e.layerY != "undefined" ? e.layerY : e.offsetY);				
            //var mouseX = (typeof e.layerY != "undefined" ? e.layerX : e.offsetX);	
            this.posX = _x;		
            this.posY = _y;
            this.boxId = boxId;
            this.html = $('#' + this.boxId).html();
            var width = $('#' + this.boxId).width();
            var height = $('#' + this.boxId).height();
            $('#' + this.boxId).empty();
            $('#popup_hidden').html(this.html);
            var pos = popup_box.show_layer(width, height);
            $('#popup_hidden').css({left: pos['left'] + 'px', top : pos['top'] + 'px'}).show();
        },		
        close_popup:function(){
            $('#' + this.boxId).html(this.html);
            $('#popup_hidden').hide().empty();			
        },
        show_layer : function (w, h){
            var result = new Array();
            var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
            var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20;
            if(this.posX + w > winwidth && this.posX >= w){
                result['left'] = this.posX - 10 - w ;
            }else if(this.posX + w > winwidth && this.posX < w){
                result['left'] = parseInt((winwidth - w)/2);
            }else{
                result['left'] = this.posX + 10;
            }
            if(this.posY + h > winheight && this.posY >= h){                
                result['top'] = ie ? (TNCom_getDocumentSize(4) + this.posY - 10 - h) : (this.posY - 10 - h);
            }else if(this.posY + h > winheight && this.posY < h){                
                result['top'] = ie ? (TNCom_getDocumentSize(4) + parseInt((winheight - h)/2)) : parseInt((winheight - h)/2);
            }else{                
                 result['top'] = ie ? (TNCom_getDocumentSize(4) + this.posY + 10) : this.posY + 10;
            }            	 
            return result;
        },
        init:function(){		
            $(function(){						
                $('body').append('<div id="popup_hidden"></div>');			    				
                    jQuery('#popup_hidden').css({'display':'none', 'position':'absolute'});
                    jQuery('#popup_hidden').css({'zIndex':'10002','overflow':'hidden'});
            });		
        }		
}
popup_box.init();